﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DataAccess_Layer
{
    public static class OrderProductDetail_DAC
    {
        public static List<OrderProductDetailDTO> get(long orderID)
        {
            List<SqlParameter> sqlParams = makeGetOrderProductDetailParams(ref orderID);
            List<OrderProductDetailDTO> prodDetails = new List<OrderProductDetailDTO>();
            OrderProductDetailDTO prodDetail = null;
            DataAccessHelper helper = null;
            SqlDataReader rdr = null;

            try
            {
                helper = new DataAccessHelper();
                rdr = helper.excuteSP("SP_Get_Order_Product_Detail", sqlParams);

                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        prodDetail = createOrderProductDetail(rdr);
                        prodDetails.Add(prodDetail);
                    }
                }
            }
            finally
            {
                helper.close();
                rdr.Close();
            }

            return prodDetails;
        }
        private static List<SqlParameter> makeGetOrderProductDetailParams(ref long orderID)
        {
            List<SqlParameter> sqlParams = new List<SqlParameter>();
            SqlParameter inParam = new SqlParameter("@orderID", SqlDbType.BigInt);
            inParam.Direction = ParameterDirection.Input;
            inParam.Value = orderID;
            sqlParams.Add(inParam);
            return sqlParams;
        }
        private static OrderProductDetailDTO createOrderProductDetail(SqlDataReader rdr)
        {
            OrderProductDetailDTO productDetail = new OrderProductDetailDTO();
            productDetail.Id = (long)rdr["ID"];
            productDetail.OrderID = (long)rdr["OrderID"];
            productDetail.ProductID = (long)rdr["ProductID"];
            productDetail.Quantity = (int)rdr["Quantity"];
            productDetail.RetailPrice = (long)rdr["RetailPrice"]; ;
            return productDetail;
        }
    }
}
